package mybatis.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import mybatis.dao.UserDao;
import mybatis.entity.User;
import mybatis.service.UserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * Author：汤小洋
 * Date：2023-07-04 16:50
 * Description：<描述>
 */
@Transactional(rollbackFor = Exception.class)
@Service
@RequiredArgsConstructor
public class UserServiceImpl  implements UserService {

    private final UserDao userDao;

    // @Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.REPEATABLE_READ,timeout = 3,rollbackFor = Exception.class)
    @Override
    public void register(User user) {
        userDao.insert(user);

        // System.out.println(5/0);
    }

    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    @Override
    public User login(String username, String password) {
        User user = userDao.selectByUsernameAndPassword(username, password);
        if (ObjectUtils.isEmpty(user)) {
            throw new RuntimeException("用户名或密码错误");
        }
        return user;
    }

    @Override
    public PageInfo<User> findPage(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<User> users = userDao.selectAll();
        return new PageInfo<>(users);
    }
}
